<?php
/**
* Georgia Institute of Technology Base PHP File
*
* This file contains global php variables, classes, and methods
* for use within the Georgia Institute of Technology website (http://www.gatech.edu).
*
* @author Office of Communications and Marketing <web@comm.gatech.edu> Bryan Jordin <bryan.jordin@comm.gatech.edu>
* @version 0.1
* @package gtbase
*/

/**
* Menu objects represent a single menu selection and will be 
* displayed as an unordered list.
* @package gtbase
* @subpackage navigation
*/
class menu {
	/**
	* An id to use to compare to the first level directory in the path
	* so it should be set as the same as the primary directory name for 
	* all NON-main menu's
	*/
	var $_id = NULL;
	
	/**
	* Determines the type of menu that will be used as the id attribute
	* @access private
	* @var string
	*/
	var $_type = NULL;
	
	/**
	* The tree objects that will make up this menu
	* @access private
	* @var array
	*/
	var $_items = array();
	
	/**
	* Determine wether this is the main menu
	* @access private
	* @var boolean
	*/
	var $_isMain = False;
	
	/**
	* Constructor sets up { @link $_id,  @link $_type }
	*/
	function menu($id, $type) {
		$this->_id = $id;	
		$this->_type = $type;
	}
	
	/**
	* Set the isMain value
	* @param boolean $main
	*/
	function setMain($main) {
		$this->_isMain = $main;
	}
	
	/**
	* Add's an item (tree object) to the menu
	* @param object $item
	*/
	function addItem($item) {
		$this->_items[] = $item;
	}
	
	/**
	* Print the menu as an unordered list with the tree elements as list items
	*/
	function printMenu() {
		if (!$this->_isMain and !in_array($this->_id, $GLOBALS['_GTSHRAPNEL'])) return;
		echo '<ul id="'.$this->_type.'">';	
		foreach($this->_items as $item) {
			echo $item->printTree();
		}
		echo '</ul>';
	}
}

/**
* Tree objects represent a menu item. They are self-containing such that
* one tree may contain one or more children that are also tree objects.
* @package gtbase
* @subpackage navigation
*/
class tree {
	/**
	* An identifier
	* @access private
	* @var string
	*/
	var $_id = NULL;
	
	/**
	* The title of the tree item to use in link representation
	* @access private
	* @var string
	*/
	var $_title = NULL;
	
	/**
	* The url that this tree item should be linked to
	* @access private
	* @var string
	*/
	var $_url = NULL;
	
	/**
	*  A collection of other tree items to show below this one
	* @access private
	* @var array
	*/
	var $_children = array();
	
	
	/**
	* Constructor sets up { @link $_id, @link $_title, @link $_url }
	*/
	function tree($id, $title, $url) {
		$this->_id = $id;
		$this->_title = $title;
		$this->_url = $url;
	}	
	
	/**
	* Adds a branch (child) to this tree
	* @param object $branch
	*/
	function addBranch($branch) {
		$this->_children[] = $branch;
	}
	
	/**
	* Test to see if the tree has children
	* @return boolean
	*/
	function hasChildren() {
		if (count($this->_children)>0) {
			return True;
		} else {
			return False;
		}
	}

	/**
	* Print tree object as an unordered list
	*/
	function printTree() {
		echo '<li>';
		echo '<a href="'.$this->_url.'"';
		if ($this->_url == $GLOBALS['_GTPATH']) { echo ' class="current"'; }
		echo '>';
		echo $this->_title;
		echo '</a>';
		if ($this->hasChildren() and in_array($this->_id, $GLOBALS['_GTSHRAPNEL'])) {
			echo '<ul>';
			foreach ($this->_children as $child) {
				echo $child->printTree();
			}
			echo '</ul>';
		}
		echo '</li>';
	}
	
}

/**
* Create a global path variable and global shrapnel variable
*/
$path = $_SERVER['REQUEST_URI'];
if(strpos($path, "index.html")) {
	$path = substr($path, 0, strpos($path, "index.html"));
} 
$path = rtrim($path, "/");
$shrapnel = explode("/", $path);
$GLOBALS['_GTPATH'] = $path;
$GLOBALS['_GTSHRAPNEL'] = $shrapnel;


/**
* Include the menu data from file
*/
include('views/inc/menuData.php');

?>
